package com.unc.allergyex.objects;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.unc.allergyex.R;

/**
 * This class is the balloon object that appears above a map pin when the pin is tapped. 
 * Its layout is specified by balloon_overlay.xml. It extends FrameLayout and is an OverlayItem.
 * It extends FrameLayout so it can dynamically create the balloon_overlay view, and it is an
 * OverlayItem so that it can be added to a map overlay.
 * @author Abigail
 *
 * @param <OverlayItem> This the Android default OverlayItem class. It is the item that is added to an ItemizedOverlay
 */
public class Balloon<OverlayItem> extends FrameLayout{

	private LinearLayout layout;
	private TextView title;
	private TextView snippet;
	private ImageView forward;
	private ImageView close;
	
	/**
	 * This method initializes the balloon. It sets the 
	 * layout of the balloon and assigns class variables to views in the
	 * balloon.
	 * 
	 * @param context Context of the parent mapview class
	 * @param balloonBottomOffset integer offset so that the balloon appears above the map pin
	 * @param currRestaurant RestaurantPin that the balloon is appearing above
	 */
	public Balloon(final Context context, int balloonBottomOffset, RestaurantPin currRestaurant) {
		super(context);
		setPadding(10, 0, 10, balloonBottomOffset);
		
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);
		
		/** create balloon */
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.balloon_overlay, layout);
		
		/** assign names to elements of ballon */
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
		forward = (ImageView) v.findViewById(R.id.start_results);
		close = (ImageView) v.findViewById(R.id.close_img_button);
		
		/** set the (non visible) restaurant ID to the id of the current restaurant */
		TextView restaurantId = (TextView) v.findViewById(R.id.restaurantId);
		restaurantId.setText(currRestaurant.getId());

		/** when you click the x image, balloon closes */
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				layout.setVisibility(GONE);
			}
		});

		/** set layout parameters of the balloon */
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;
		addView(layout, params);
	}

	/**
	 * This method displays sets each field to contain restaurant information
	 * It hides restaurant information for the 'you are here' pin
	 * @param restaurant
	 */
	public void setData(RestaurantPin restaurant){
		
		layout.setVisibility(VISIBLE);
		if (restaurant.getName() != null){
			title.setVisibility(VISIBLE);
			title.setText(restaurant.getName());
		}
		else{
			title.setVisibility(GONE);			
		}
		if (restaurant.getAddress() != null){
			snippet.setVisibility(VISIBLE);
			snippet.setText(restaurant.getAddress());
		}
		else{
			snippet.setVisibility(GONE);			
		}
		if (restaurant.getSnippet() == "hide"){
			/**this condition is only true for the red 'you are here' pin */ 
			forward.setVisibility(GONE);
		}

	}

}
